package com.example.snashuitraverl.demos.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.snashuitraverl.demos.aop.LogAnnotation;
import com.example.snashuitraverl.demos.common.BaseResponse;
import com.example.snashuitraverl.demos.common.ErrorCode;
import com.example.snashuitraverl.demos.common.ResultUtils;
import com.example.snashuitraverl.demos.domain.DTO.UserDto;
import com.example.snashuitraverl.demos.domain.Request.UserRequest;
import com.example.snashuitraverl.demos.domain.User;
import com.example.snashuitraverl.demos.domain.VO.UserVO;
import com.example.snashuitraverl.demos.exception.BusinessException;
import com.example.snashuitraverl.demos.mapper.UserMapper;
import com.example.snashuitraverl.demos.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@ApiOperation(value = "管理用户端")
@RestController
@Slf4j
public class UserController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserService userService;

    @ApiOperation(value = "根据ID进行查询")
    @PostMapping("/selectByOpneID")
    public BaseResponse<User> selectByOpenID(@RequestBody String openID) {
        String substring = openID.substring(11, openID.length() - 2);
        if (openID == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (StringUtils.isBlank(openID)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(userService.selectOpneId(substring), "成功");
    }

    @ApiOperation(value = "管理端的管理员的查询所有")
    @GetMapping("/selectAll")
    public BaseResponse<Page<User>> selectAll(UserDto userDto, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (loginUser.getRole() != 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "权限不够");
        }
        return ResultUtils.success(userService.selectAll(userDto, request), "查询成功");
    }

    @LogAnnotation("管理员登录")
    @ApiOperation(value = "管理端的登录")
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserRequest userRequest, HttpServletRequest request) {
        String account = userRequest.getAccount();
        String password = userRequest.getPassword();
        if (StringUtils.isAnyBlank(account, password)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号密码不能不写");
        }
        if (account == null || password == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不能传null");
        }
        User user = userService.userLogin(account, password, request);
        if (user == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "登录失败");
        }
        return ResultUtils.success(user, "登录成功");
    }

    @PostMapping("/select")
    public List<User> users() {
        return userMapper.selectList(new QueryWrapper<User>());
    }

    @ApiOperation(value = "支付宝用户的修改账号")
    @PostMapping("/updateByUser")
    public BaseResponse<Integer> updateByUser(@RequestBody UserVO userVO, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户登录错误");
        }

        return userService.updateUser(userVO, request)==1?ResultUtils.success(userService.updateUser(userVO, request),"成功"): ResultUtils.error(ErrorCode.PARAMS_ERROR,"登录错误");
    }
    @ApiOperation(value = "支付宝用户的修改账号")
    @PostMapping("/updateByAliUser")
    public BaseResponse<Integer> updateByAliUser(@RequestBody User user) {
        return userService.updateAliUser(user)==1?ResultUtils.success(userService.updateAliUser(user),"成功"): ResultUtils.error(ErrorCode.PARAMS_ERROR,"登录错误");
    }
    @ApiOperation(value = "禁止登录")
    @GetMapping("/ForbidUser")
    public BaseResponse<String> ForbidUser(@RequestParam String account) {
        return userService.forbidLogin(account) == 1 ? ResultUtils.success("修改成功") : ResultUtils.error(ErrorCode.PARAMS_ERROR, "修改失败");
    }
    @ApiOperation(value = "打开登录")
    @GetMapping("/openUser")
    public BaseResponse<String> openUser(@RequestParam String account) {
        return userService.openUserLogin(account) == 1 ? ResultUtils.success("修改成功") : ResultUtils.error(ErrorCode.PARAMS_ERROR, "修改失败");
    }
//    @Resource
//    private SparkManager sparkManager;
//    @PostMapping("/chat")
//    public BaseResponse<String> chatManager(@RequestBody String message){
//        log.info(message);
//        String s = JSONUtil.toJsonStr(message);
//        log.info(s);
//        return ResultUtils.success(sparkManager.sendMesToAIUseXingHuo(s),"成功");
//    }
}
